﻿/*!
 * 远光ECP平台3.4版本 
 * 
 * 复选择框.：qzz.ui.input.checkbox
 * ecp.qzz.ui.input.checkbox.js
 * @requires qzz.ui
 */

/**
 *复选择框名空间
 *@name qzz.ui.input
 *@namespace qzz.ui.input
 */
define(["qzz.window"], function() {
	
	var qzz = window.qzz;
	
	qzz.space("ui.input");
	
	/**
	 @description 复选择框 
	<pre> 
	此类作为复选择框。
	</pre>
	@class qzz.ui.input.checkbox 复选择框
	@extends qzz.window
	@property
	@example
	@author qinzhenzhou@ygsoft.com
	@version 1.0
	@date 2017-05-31
	*/	
	qzz.ui.input.checkbox = qzz.Class(qzz.ui.window, 
	/**@lends qzz.ui.input.checkbox# */	
	{
	    /**
	     * 对象创建函数.
	     */
	    _create:function() {
            this.className = "CheckBox";            
            this.base();
            this.getOption().width = this.getNumberValue(this.getOption().width, 20);
            this.getOption().height = this.getNumberValue(this.getOption().height, 30);
            if(this.getOption().height < 16) {
            	this.getOption().height = 16;
            }
            this.getOption().borderWidth = 0;
            this.getOption().text = this.getOption().text || this.getOption().caption || "";
            this.getOption().checked = this.getOption().checked === true;
            this.getOption().disabled = this.getOption().disabled === true;
            this.getOption().checkboxCss = this.getOption().checkboxCss || ["qzz_checked","qzz_unchecked","qzz_somechecked"];
            this.getOption().radioCss = this.getOption().radioCss || ["qzz_radio_checked","qzz_radio_unchecked","qzz_radio_somechecked"];
            this.getOption().somechecked = this.getOption().somechecked === true;
            
            this.getOption().type = this.getOption().type || "checkBox";
	    },
	    addToRadioList:function() {
	    	var name = this.thisWindow.getAttribute("name");
        	if(name != null && name !== "") {
        	    if(qzz.ui.input._radioList == null) {
        	    	qzz.ui.input._radioList = {};
        	    }
        	    if(qzz.ui.input._radioList[name] == null) {
        	    	qzz.ui.input._radioList[name] = [];
        	    }
        	    qzz.ui.input._radioList[name].push(this);
        	}
	    },
	    delFromRadioList:function() {
	    	var name = this.thisWindow.getAttribute("name");
        	if(name != null && name !== "") {
        	    if(qzz.ui.input._radioList == null) {
        	    	qzz.ui.input._radioList = {};
        	    }
        	    if(qzz.ui.input._radioList[name] == null) {
        	    	qzz.ui.input._radioList[name] = [];
        	    }
        	    var rl = qzz.ui.input._radioList[name];
        	    for(var i = 0, ilen = rl.length; i < ilen; i++) {
        	    	var cc = rl[i];
        	    	if(cc.index == this.index) {
        	    		rl.splice(i, 1);
        	    		break;
        	    	}
        	    }
        	}
	    },
	    uncheckOtherRadio:function() {
	    	var name = this.thisWindow.getAttribute("name");
        	if(name != null && name !== "") {
        	    if(qzz.ui.input._radioList == null) {
        	    	qzz.ui.input._radioList = {};
        	    }
        	    if(qzz.ui.input._radioList[name] == null) {
        	    	qzz.ui.input._radioList[name] = [];
        	    }
        	    var rl = qzz.ui.input._radioList[name];
        	    for(var i = 0, ilen = rl.length; i < ilen; i++) {
        	    	var cc = rl[i];
        	    	if(cc.index != this.index && cc.getCheck() === true) {
        	    		cc.setCheck(false);
        	    		cc.trigger("onClick", [cc.getOption().checked]);
        	    	}
        	    }
        	}
	    },
	    /**
	     * 渲染函数.
	     */
	    _render:function() {
	    	//兼容用Input配置的情况.
	    	var placeholder = null;
	    	if(this.control != null) {
	    	    placeholder = this.control.attr("placeholder");
	    	}
	    	if(this.control && this.control[0].tagName === "INPUT") {
	    		var conDiv = $(this.createElement("div"));
	    		conDiv.attr("dataField", this.control.attr("dataField"));
	    		conDiv.attr("data-option", this.control.attr("data-option"));
	    		conDiv.attr("id", this.control.attr("id"));
	    		conDiv.attr("name", this.control.attr("name"));
	    		conDiv.attr("width", this.control.attr("width"));
	    		//if()
	    		//this.getOption().type
	    		this.control.replaceWith(conDiv);
	    		this.control = conDiv;
	    		this.control.data("_ecpUI", this);
	    		this.thisWindow = conDiv[0];
	    	}
	    	placeholder =  placeholder || this.getOption().placeholder;
	    	
	    	this.base();	    	
	    	this._tab = this.createElement("TABLE");
	    	var cap = this.thisWindow.innerHTML;
	    	this.thisWindow.innerHTML = "";
	    	if(this.getOption().width < 16) {
	    		this.getOption().width = 16;
	    		this.thisWindow.style.width = "";
            }
	    	this.thisWindow.style.top = "2px";
	    	this.addStyle(this.thisWindow, "checkBoxDiv");
	    	this.thisWindow.appendChild(this._tab);
	    	this.setStyle(this._tab, "checkBoxTab");
	    	var tr = this._tab.insertRow(0);
	    	this.checkBoxTd = tr.insertCell(0);
	    	this.setStyle(this.checkBoxTd, "checkBoxTd");
	    	this.checkBoxDiv = this.createElement("DIV");
	    	this.checkBoxTd.appendChild(this.checkBoxDiv);
	    	this.setStyle(this.checkBoxDiv, "qzz_unchecked");
	    	var _this = this;
//	    	this.checkBoxDiv.onclick = function() {
//	    		//_this.click();
//	    	}
	    	this.checkBoxDiv.onmousedown = function() {
	    		_this.click();	    		
	    	}
	    	this.textTd = tr.insertCell(1);
	    	if(this.getOption().text == null || this.getOption().text === "") {
	    	    this.getOption().text = cap;
	    	}
	    	this.textTd.onmouseup = function() {
	    		_this.click();
	    	}
	    	this.setStyle(this.textTd,"qzz_check_text");
	    	if(this.getOption().type == "radio") {
	        	this.addToRadioList();            	
	        }
	    	this.refresh();
	    },
	    /**
	     * 点击按钮.
	     */
	    click:function() {
	    	if(this.getOption().disabled !== true) {
	    		if(this.getOption().type == "checkBox" 
	    			|| this.getOption().type == "radio" && this.getOption().checked !== true
	    			    || this.getOption().checked !== true && this.getOption().somechecked === true) {
		    	    this.getOption().checked = !(this.getOption().checked === true);
		    	    this.getOption().somechecked = false;
		    	    this.refresh();	 
		    	    this.trigger("onClick", [this.getOption().checked]);
		    	    if(this.getOption().type === "radio") {
		    	    	this.uncheckOtherRadio();
		    	    }
	    		}
	    	}
	    		
	    },	    
	    
	    
	    refresh:function() {
	    	var index = 1;
	    	if(this.getOption().checked === true) {
	    		index = 0;
	    	}
            if(this.getOption().type === "checkBox" 
            	&& this.getOption().somechecked === true) {
		    	index = 2;
	    	}
            if(this.getOption().type === "checkBox") {
	    	    this.setStyle(this.checkBoxDiv, this.getOption().checkboxCss[index]);
            } else {
            	this.setStyle(this.checkBoxDiv, this.getOption().radioCss[index]);
            }
	    	if(this.getOption().text != null && this.getOption().text !== "") {
	    		this.textTd.style.display = "";
	    	    this.textTd.innerHTML = this.getOption().text;
	    	    this.thisWindow.style.width = "";
	    	} else {
	    		this.textTd.style.display = "none";
	    		this.thisWindow.style.width = "16px";
	    	}
	    	if(this.getOption().disabled === true) {
	    		this.addStyle(this.textTd, "disabled");
	    		this.addStyle(this.checkBoxDiv, "disabled");
	    	}
	    },
	    
	    getValue:function() {
	    	return this.value();
	    },
	    
	    setValue:function(val) {
	    	this.value(val)
	    	this.refresh();
	    },
	    
	    setText:function(text) {
	    	this.getOption().text = text;
	    	this.refresh();
	    },
	    
	    setDisable:function(val) {
	    	this.getOption().disabled = val === true;
	    	this.refresh();
	    },
	    getDisable:function() {
	    	return this.getOption().disabled === true;
	    },
	    
	    setCheck:function(val) {
	    	this.value(val);
	    },
	    getCheck:function() {
	    	return this.value();
	    },	    
	    setSomeCheck:function(value) {
	    	this.getOption().somechecked = value === true;
	    	this.refresh();
	    },
	    
	    setType:function(type) {
	    	this.getOption().type = type || "checkBox";
	    	if(this.getOption().type === "radio") {
	    		this.addToRadioList();
	    	} else {
	    		this.delFromRadioList();
	    	}
	    	this.refresh();
	    },
	    
	    getType:function() {
	    	return this.getOption().type;
	    },
	    
	    value:function(value) {
	    	if(value == null) {
	    		return this.getOption().checked;
	    	} else {
	    		this.getOption().checked = value === true;
	    		this.refresh();
	    	}
	    }	    
	});
	/**
	 * 文本控件.
	 */
	_bindRenderFunc("qzzcheckbox", qzz.ui.input.checkbox);
	return qzz.ui.input.checkbox;
});